package com.aiyige.page.interest.util;

import android.text.TextUtils;

import com.aiyige.model.moment.backup.TagBackup;
import com.aiyige.model.tag.Tag;
import com.aiyige.page.interest.model.Interest;
import com.aiyige.setup.model.Setup;
import com.aiyige.setup.util.SetupUtil;
import com.aiyige.utils.ListUtil;
import com.j256.ormlite.stmt.query.In;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class InterestUtil {
    public static final int MAX_SELECTED_PUBLISH_INTEREST_NUM = 7;
    public static final int MAX_SELECTED_USER_INTEREST_NUM = 7;
    public static final int MAX_SELECTED_CHANNEL_NUM = 7;
    public static final int MIN_CHANGE_NUM = 8;
    public static final int MAX_SELECTED_INTEREST_NUM = 7;

    public static int findIndex(List<Interest> list, Interest target) {
        if (ListUtil.isEmpty(list) || target == null) return -1;
        int upLimit = list.size();
        for (int i = 0; i < upLimit; ++i) {
            Interest interest = list.get(i);
            if (target.equals(interest)) {
                return i;
            }
        }
        return -1;
    }

    public static int unselect(List<Interest> list, Interest target) {
        if (ListUtil.isEmpty(list) || target == null) return -1;
        int upLimit = list.size();
        for (int i = 0; i < upLimit; ++i) {
            Interest interest = list.get(i);
            if (target.equals(interest)) {
                interest.setSelected(false);
                return i;
            }
        }
        return -1;
    }

    public static int select(List<Interest> list, Interest target) {
        if (ListUtil.isEmpty(list) || target == null) return -1;
        int upLimit = list.size();
        for (int i = 0; i < upLimit; ++i) {
            Interest interest = list.get(i);
            if (target.equals(interest)) {
                interest.setSelected(true);
                return i;
            }
        }
        return -1;
    }


    public static boolean checkEqual(List<Interest> sourceList, List<Interest> destList) {
        if (ListUtil.isEmpty(sourceList) && ListUtil.isEmpty(destList)) return true;
        if (ListUtil.isEmpty(sourceList) || ListUtil.isEmpty(destList)) return false;
        if (sourceList.size() != destList.size()) return false;
        outLoop:
        for (Interest src : sourceList) {
            for (Interest dest : destList) {
                if (src.getId() == null && dest.getId() == null) continue outLoop;
                if (src.getId() == null || dest.getId() == null) continue;
                if (src.getId().equals(dest.getId())) continue outLoop;
            }
            return false;
        }
        return true;
    }

    public static int countOfSelected(List<Interest> interestList) {
        if (ListUtil.isEmpty(interestList)) return 0;
        int count = 0;
        for (Interest interest : interestList) {
            if (interest.isSelected()) count++;
        }
        return count;
    }

    public static List<String> extractIdList(List<Interest> interestList) {
        List<String> idList = new LinkedList<>();
        for (Interest interest : interestList) {
            if (TextUtils.isEmpty(interest.getId())) continue;
            idList.add(interest.getId());
        }
        return idList;
    }

    public static List<Interest> extractSelected(List<Interest> interestList) {
        List<Interest> selectedList = new LinkedList<>();
        for (Interest interest : interestList) {
            if (interest.isSelected()) {
                selectedList.add(new Interest(interest));
            }
        }
        return selectedList;
    }

    public static void clearSelect(List<Interest> targetList) {
        for (Interest interest : targetList) {
            interest.setSelected(false);
        }
    }

    public static void resumeSelect(List<Interest> targetList, List<Interest> selectedList) {
        if (ListUtil.isEmpty(selectedList)) return;
        if (ListUtil.isEmpty(targetList)) return;
        outLoop:
        for (Interest selectedItem : selectedList) {
            for (Interest targetItem : targetList) {
                if (targetItem.equals(selectedItem)) {
                    targetItem.setSelected(true);
                    continue outLoop;
                }
            }
        }
    }

    public static Tag convertInterestToTag(Interest interest) {
        if (interest == null) return null;
        Tag tag = null;
        switch (interest.getCategory()) {
            case Interest.CATEGORY_INDUSTRY:
                tag = Tag.newBuilder()
                        .id(interest.getId())
                        .sourceId(interest.getSourceId())
                        .name(interest.getTitle())
                        .groupType(SetupUtil.GROUP_TYPE_CHANNEL)
                        .userSelect(!interest.isRecommend())
                        .build();
                break;
            case Interest.CATEGORY_INTEREST:
                tag = Tag.newBuilder()
                        .id(interest.getId())
                        .sourceId(interest.getSourceId())
                        .name(interest.getTitle())
                        .groupType(SetupUtil.GROUP_TYPE_AVOCATION)
                        .userSelect(!interest.isRecommend())
                        .build();
                break;
            default:
                break;
        }
        return tag;
    }


    public static TagBackup convertInterestToTagBackup(Interest interest) {
        if (interest == null) return null;
        TagBackup tagBackup = null;
        switch (interest.getCategory()) {
            case Interest.CATEGORY_INDUSTRY:
                tagBackup = TagBackup.newBuilder()
                        .id(interest.getId())
                        .sourceId(interest.getSourceId())
                        .name(interest.getTitle())
                        .groupType(SetupUtil.GROUP_TYPE_CHANNEL)
                        .userSelect(!interest.isRecommend())
                        .build();
                break;
            case Interest.CATEGORY_INTEREST:
                tagBackup = TagBackup.newBuilder()
                        .id(interest.getId())
                        .sourceId(interest.getSourceId())
                        .name(interest.getTitle())
                        .groupType(SetupUtil.GROUP_TYPE_AVOCATION)
                        .userSelect(!interest.isRecommend())
                        .build();
                break;
            default:
                break;
        }
        return tagBackup;
    }

    public static String joinInterestListTitle(String delimiter, List<Interest> interestList) {
        StringBuilder sb = new StringBuilder();
        Iterator<Interest> it = interestList.iterator();
        if (it.hasNext()) {
            sb.append(it.next().getTitle());
            while (it.hasNext()) {
                sb.append(delimiter);
                sb.append(it.next().getTitle());
            }
        }
        return sb.toString();
    }

    public static Interest convertTagToInterest(Tag tag) {
        if (tag == null) return null;
        if (TextUtils.isEmpty(tag.getGroupType())) {
            tag.setGroupType(SetupUtil.GROUP_TYPE_AVOCATION);
        }
        if (TextUtils.isEmpty(tag.getId()) || TextUtils.isEmpty(tag.getName()) || TextUtils.isEmpty(tag.getGroupType()))
            return null;
        Interest interest = null;
        switch (tag.getGroupType()) {
            case SetupUtil.GROUP_TYPE_CHANNEL:
                interest = Interest.newBuilder()
                        .id(tag.getId())
                        .sourceId(tag.getSourceId())
                        .category(Interest.CATEGORY_INDUSTRY)
                        .title(tag.getName())
                        .recommend(!tag.isUserSelect())
                        .build();
                break;
            case SetupUtil.GROUP_TYPE_AVOCATION:
                interest = Interest.newBuilder()
                        .id(tag.getId())
                        .sourceId(tag.getSourceId())
                        .category(Interest.CATEGORY_INTEREST)
                        .title(tag.getName())
                        .recommend(!tag.isUserSelect())
                        .build();
                break;
        }
        return interest;
    }


    public static Interest convertTagBackupToInterest(TagBackup tagBackup) {
        if (tagBackup == null) return null;
        if (TextUtils.isEmpty(tagBackup.getGroupType())) {
            tagBackup.setGroupType(SetupUtil.GROUP_TYPE_AVOCATION);
        }
        if (TextUtils.isEmpty(tagBackup.getId()) || TextUtils.isEmpty(tagBackup.getName()) || TextUtils.isEmpty(tagBackup.getGroupType()))
            return null;
        Interest interest = null;
        switch (tagBackup.getGroupType()) {
            case SetupUtil.GROUP_TYPE_CHANNEL:
                interest = Interest.newBuilder()
                        .id(tagBackup.getId())
                        .sourceId(tagBackup.getSourceId())
                        .category(Interest.CATEGORY_INDUSTRY)
                        .title(tagBackup.getName())
                        .recommend(!tagBackup.isUserSelect())
                        .build();
                break;
            case SetupUtil.GROUP_TYPE_AVOCATION:
                interest = Interest.newBuilder()
                        .id(tagBackup.getId())
                        .sourceId(tagBackup.getSourceId())
                        .category(Interest.CATEGORY_INTEREST)
                        .title(tagBackup.getName())
                        .recommend(!tagBackup.isUserSelect())
                        .build();
                break;
        }
        return interest;
    }

    public interface ConvertToInterestPostExecuteCallBack {
        Interest postExecute(Interest interest);
    }

    public static List<Interest> convertTagListToInterestList(List<Tag> tagList, ConvertToInterestPostExecuteCallBack callBack) {
        List<Interest> interestList = new LinkedList<>();
        if (ListUtil.isEmpty(tagList)) return interestList;
        for (Tag tag : tagList) {
            Interest interest = convertTagToInterest(tag);
            if (interest == null) continue;
            interest = callBack.postExecute(interest);
            if (interest == null) continue;
            interestList.add(interest);
        }
        return interestList;
    }

    public static List<Interest> convertTagBackupListToInterestList(List<TagBackup> tagBackupList, ConvertToInterestPostExecuteCallBack callBack) {
        List<Interest> interestList = new LinkedList<>();
        if (ListUtil.isEmpty(tagBackupList)) return interestList;
        for (TagBackup tagBackup : tagBackupList) {
            Interest interest = convertTagBackupToInterest(tagBackup);
            if (interest == null) continue;
            interest = callBack.postExecute(interest);
            if (interest == null) continue;
            interestList.add(interest);
        }
        return interestList;
    }


    public static List<Interest> convertTagListToInterestList(List<Tag> tagList) {
        List<Interest> interestList = new LinkedList<>();
        if (ListUtil.isEmpty(tagList)) return interestList;
        for (Tag tag : tagList) {
            Interest interest = convertTagToInterest(tag);
            if (interest == null) continue;
            interestList.add(interest);
        }
        return interestList;
    }

    public static ArrayList<Interest> extractSelectedData(List<Interest> pool) {
        ArrayList<Interest> selectedData = new ArrayList<>();
        for (Interest interest : pool) {
            if (interest.getType() != Interest.TYPE_NORMAL) continue;
            selectedData.add(interest);
        }
        return selectedData;
    }

    public static List<Tag> convertInterestListToTagList(List<Interest> interestList) {
        List<Tag> tagList = new LinkedList<>();
        if (ListUtil.isEmpty(interestList)) return tagList;
        for (Interest interest : interestList) {
            if (interest.getType() != Interest.TYPE_NORMAL) continue;
            tagList.add(convertInterestToTag(interest));
        }
        return tagList;
    }


    public static List<Interest> convertTagBackupListToInterestList(List<TagBackup> tagBackupList) {
        List<Interest> interestList = new LinkedList<>();
        if (ListUtil.isEmpty(tagBackupList)) return interestList;
        for (TagBackup tagBackup : tagBackupList) {
            Interest interest = convertTagBackupToInterest(tagBackup);
            if (interest == null) continue;
            interestList.add(interest);
        }
        return interestList;
    }

    public static List<TagBackup> convertInterestListToTagBackupList(List<Interest> interestList) {
        List<TagBackup> tagBackupList = new LinkedList<>();
        if (ListUtil.isEmpty(interestList)) return tagBackupList;
        for (Interest interest : interestList) {
            if (interest.getType() != Interest.TYPE_NORMAL) continue;
            tagBackupList.add(convertInterestToTagBackup(interest));
        }
        return tagBackupList;
    }


}
